﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.BLL.SchedulingSystem;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace AdverseEventReportSystem.Web.Areas.Scheduling.Controllers
{
    public class SchedulingSignInController : BaseController
    {
        // GET: Scheduling/SchedulingSignIn
        public ActionResult Index()
        {
            DateTime startDate = DateTime.Now.AddDays(1 - Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d")));
            DateTime endDate = startDate.AddDays(6);
            ViewBag.StartDate = startDate.ToString("yyyy-MM-dd");
            ViewBag.EndDate = endDate.ToString("yyyy-MM-dd");
            ViewBag.JobCode = loginUser.JobCode;
            return View();
        }

        /// <summary>
        /// 获取班别列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetSchedulingCategoryList()
        {
            DataTable dt = new DataTable();
            SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
            ResultHelperOfDataTable result = service.GetSchedulingCategoryList(loginUser.CurrDepCode);
            if (result.S)
            {
                string code = WebUtils.GetString("code");
                var d = result.D.AsEnumerable().Where(x => DataConvert.StrToInt(x["Enabled"]) == 1 && DataConvert.StrToInt(x["IsDel"]) == 0 && (string.IsNullOrEmpty(code) ? true : x["Code"].ToString().Contains(code)));
                if (d.Count() > 0)
                {
                    dt = d.CopyToDataTable<DataRow>();
                }
            }
            return JsonDate(new { Rows = dt });
        }

        /// <summary>
        /// 签到列表
        /// </summary>
        /// <returns></returns>
        public string Search()
        {
            int pageIndex = DataConvert.StrToInt(Request["page"], 1);
            int pageSize = DataConvert.StrToInt(Request["pageSize"], ConfigHelper.GetConfigInt("pagesize"));
            string startDate = WebUtils.GetString("startDate");
            string endDate = WebUtils.GetString("endDate");
            int isChange = WebUtils.GetInt("isChange", -1);
            string name = WebUtils.GetString("name");

            string personnelId = "";
            if (loginUser.JobCode != "护士长")
            {
                personnelId = loginUser.Personnel_id;
            }

            SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
            ResultHelperOfString serviceResult = service.GetSchedulingSignInList(DataConvert.StrToDateTime(startDate, DateTime.Now), DataConvert.StrToDateTime(endDate, DateTime.Now), loginUser.CurrDepCode, isChange, personnelId, name, pageIndex, pageSize);
            var result = "";
            if (serviceResult.S)
            {
                result = serviceResult.D;
            }

            return result;
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id = 0)
        {
            SchedulingSignIn signIn = new SchedulingSignIn();
            if (id == 0)
            {
                DateTime signInDate = DataConvert.StrToDateTime(DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now);
                signIn.SignInDate = signInDate;
                SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
                ResultHelperOfSchedulingSignIn result = service.GetSchedulingSignIn(signInDate, loginUser.Personnel_id, loginUser.CurrDepCode);
                if (result.S)
                {
                    if (result.D == null)
                    {
                        ResultHelperOfScheduling result1 = service.GetScheduling(signInDate, loginUser.Personnel_id, loginUser.CurrDepCode);
                        if (result1.S)
                        {
                            if (result1.D != null)
                            {
                                signIn.OldCategoryId = result1.D.CategoryId;
                            }
                        }
                    }
                    else
                    {
                        signIn = result.D;
                    }
                }
            }

            ViewBag.SignIn = signIn;
            return View();
        }

        /// <summary>
        /// 添加/编辑签到
        /// </summary>
        /// <param name="signIn"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(SchedulingSignIn signIn)
        {
            OperateResult resultInfo = SchedulingSignInManager.Save(signIn);
            return JsonDate(resultInfo);
        }
    }
}

